import 'package:flutter/material.dart';

class CustomList<T> extends StatelessWidget {
  final Widget Function(T item, int index) itemBuilder;
  final List<T> items;
  final Widget Function(T item, int index)? separatorBuilder;
  final Function(T item, int index)? onItemTap;
  final Function(T item, int index)? onLongPress;
  final ScrollPhysics? physics;
  final bool shrinkWrap;
  final EdgeInsetsGeometry? padding;
  final Axis scrollDirection;
  final ScrollController? controller;

  const CustomList({
    super.key,
    required this.itemBuilder,
    required this.items,
    this.separatorBuilder,
    this.onItemTap,
    this.physics,
    this.shrinkWrap = false,
    this.padding,
    this.scrollDirection = Axis.vertical,
    this.onLongPress,
    this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      controller: controller,
      itemBuilder: (ctx, index) {
        return GestureDetector(
          onTap: () => onItemTap?.call(items[index], index),
          onLongPress: () => onLongPress?.call(items[index], index),
          child: itemBuilder(items[index], index),
        );
      },
      separatorBuilder: (ctx, index) {
        return separatorBuilder?.call(items[index], index) ?? const SizedBox();
      },
      itemCount: items.length,
      physics: physics,
      shrinkWrap: shrinkWrap,
      padding: padding ?? EdgeInsets.zero,
      scrollDirection: scrollDirection,
    );
  }
}

class CustomSliverList<T> extends StatelessWidget {
  final Widget Function(T item, int index) itemBuilder;
  final List<T> items;
  final Function(T item, int index)? onItemTap;
  final Function(T item, int index)? onLongPress;

  const CustomSliverList({
    super.key,
    required this.itemBuilder,
    required this.items,
    this.onItemTap,
    this.onLongPress,
  });

  @override
  Widget build(BuildContext context) {
    return SliverList(
      delegate: SliverChildBuilderDelegate(
        (ctx, index) {
          return GestureDetector(
            onTap: () => onItemTap?.call(items[index], index),
            onLongPress: () => onLongPress?.call(items[index], index),
            child: itemBuilder(items[index], index),
          );
        },
        childCount: items.length,
      ),
    );
  }
}

ListView buildList<T>({
  required List<T> items,
  required Widget Function(T item, int index) itemBuilder,
  Widget Function(T item, int index)? separatorBuilder,
  Function(T item, int index)? onItemTap,
  Function(T item, int index)? onLongPress,
  ScrollPhysics? physics,
  bool shrinkWrap = false,
  EdgeInsetsGeometry? padding,
  Axis scrollDirection = Axis.vertical,
  ScrollController? controller,
}) {
  return ListView.separated(
    controller: controller,
    itemBuilder: (ctx, index) {
      return GestureDetector(
        onTap: () => onItemTap?.call(items[index], index),
        onLongPress: () => onLongPress?.call(items[index], index),
        child: itemBuilder(items[index], index),
      );
    },
    separatorBuilder: (ctx, index) {
      return separatorBuilder?.call(items[index], index) ?? const SizedBox();
    },
    itemCount: items.length,
    physics: physics,
    shrinkWrap: shrinkWrap,
    padding: padding ?? EdgeInsets.zero,
    scrollDirection: scrollDirection,
  );
}

SliverList buildSliverList<T>({
  required List<T> items,
  required Widget Function(T item, int index) itemBuilder,
  Function(T item, int index)? onItemTap,
  Function(T item, int index)? onLongPress,
}) {
  return SliverList(
    delegate: SliverChildBuilderDelegate(
      (ctx, index) {
        return GestureDetector(
          onTap: () => onItemTap?.call(items[index], index),
          onLongPress: () => onLongPress?.call(items[index], index),
          child: itemBuilder(items[index], index),
        );
      },
      childCount: items.length,
    ),
  );
}
